﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using PBO.Data;
using PokemonBattle.BattleNetwork;

namespace PBO.Game.BattleClass
{
    public partial class Battle
    {

        private bool BeginAttack(Move selMove, Pokemon user, Pokemon target, bool moveCall, bool multiTarget)
        {
            var selMoveType = GetAttackMoveType(selMove, user);

            //避雷针,吸水
            if (target.Friend != null && target.Friend.HealthPower != 0 && ((selMoveType == BattleType.Electric && target.Friend.GetSelTrait() == Trait.Lightningrod && target.GetSelTrait() != Trait.Lightningrod) || (selMoveType == BattleType.Water && target.Friend.GetSelTrait() == Trait.StormDrain && target.GetSelTrait() != Trait.StormDrain)))
            {
                target.Friend.RaiseTrait();
                return false;
            }

            double typeEff = CalculateAttackTypeEffect(user, target, selMoveType);
            if (typeEff == 0)
            {
                log.AppendMsg("NoEffect", target.GetNameString());
                //log.AddText("这对" + target.GetNameString() + "没有效果...", Color.Red);
                return false;
            }
            if (typeEff <= 1 && target.GetSelTrait() == Trait.WonderGuard && user.GetSelTrait() != Trait.MoldBreaker && selMoveType != BattleType.None)
            {
                target.ShowTrait();
                log.AppendMsg("NoEffect", target.GetNameString());
                //log.AppendText(target.GetNameString() + "的属性之盾特性使得攻击无效！");
                return false;
            }

            double acc = CalculateAcc(user, target, selMove);
            if (acc < 0)
            {
                Fail();
                return false;
            }
            if (_random.NextDouble() > acc)
            {
                log.AppendMsg("Miss", target.GetNameString());
                //log.AppendText(user.CustomName + "的技能没有命中...");
                return false;
            }
            else
            {
                Attack(user, target, selMove, typeEff, moveCall, multiTarget);
            }
            return true;
        }

        #region hit

        private static int Get2to5Hit(Random rnd)
        {
            int r = rnd.Next(8);
            if (r > 5)
            {
                return r - 2;
            }
            else
            {
                return r > 2 ? 3 : 2;
            }
        }

        public static int GetHit(Random rnd, Trait ability, MoveAdditionalEffect eff)
        {
            switch (eff)
            {
                case MoveAdditionalEffect.二连击:
                    return 2;
                case MoveAdditionalEffect.三连踢:
                    return 3;
                case MoveAdditionalEffect.二至五连击:

                    if (ability == Trait.SkillLink)
                    {
                        return 5;
                    }
                    else
                    {
                        return Get2to5Hit(rnd);
                    }
                default:
                    return 1;
            }
        }

        #endregion

        private void Attack(Pokemon user, Pokemon target, Move selMove, double typeEff, bool moveCall, bool multiTarget)
        {
            var selMoveType = GetAttackMoveType(selMove, user);

            int hit = GetHit(_random, user.GetSelTrait(), selMove.Data.AddEffect2);

            if (selMove.Data.AddEffect2 == MoveAdditionalEffect.破墙)
            {
                target.MyTeam.Group.BreakWall();
            }

            int damage = 0;
            bool hpMax = (target.HealthPower == target.MaxHp);

            for (int i = 1; i <= hit; i++)
            {
                if (hit > 1) log.AppendMsg("Hits", i);
                //if (hit > 1) log.AddText(i + "连击！");
                damage = CalculateDamage(selMove, typeEff, selMoveType, user, target, true, multiTarget, i);
                if (damage == 0) return;
                HurtTarget(user, target, ref damage, selMove, moveCall, hpMax);
                if (target.HealthPower == 0 || user.HealthPower == 0)
                    break; // TODO: might not be correct. Was : Exit For
            }

            if (user.HealthPower > 0)
            {
                int MoveAdditionalEffectOdds = selMove.Data.AddEffectOdds;
                if (user.GetSelTrait() == Trait.SereneGrace || user.MyTeam.Group.rainbow)
                    MoveAdditionalEffectOdds *= 2;
                switch (selMove.Data.AddEffect1)
                {
                    case MoveAdditionalEffect.一定几率自身物攻加一:
                        if (this.RandomHappen(MoveAdditionalEffectOdds))
                            user.AbilityLvUpChanged(StatType.Atk, 1, false);
                        break;
                    case MoveAdditionalEffect.一定几率自身特攻加一:
                        if (this.RandomHappen(MoveAdditionalEffectOdds))
                            user.AbilityLvUpChanged(StatType.SpAtk, 1, false);
                        break;
                    case MoveAdditionalEffect.一定几率自身物防加一:
                        if (this.RandomHappen(MoveAdditionalEffectOdds))
                            user.AbilityLvUpChanged(StatType.Def, 1, false);
                        break;
                    //case MoveAdditionalEffect.一定几率自身速度加一:
                    //    if (this.RandomHappen(MoveAdditionalEffectOdds))
                    //        user.SpeedLVUp(1, false);
                    //    break;
                    case MoveAdditionalEffect.一定几率所有能力加一:
                        if (this.RandomHappen(MoveAdditionalEffectOdds))
                        {
                            foreach (var stat in BattleStats.Type5D)
                            {
                                user.AbilityLvUpChanged(stat, 1, false);
                            }
                        }
                        break;
                    case MoveAdditionalEffect.自身特攻减二:
                        user.AbilityLvDownChanged(StatType.SpAtk, 2, false);
                        break;
                    case MoveAdditionalEffect.自身物理攻防减一:
                        user.AbilityLvDownChanged(StatType.Atk, 1, false);
                        user.AbilityLvDownChanged(StatType.Def, 1, false);
                        break;
                    case MoveAdditionalEffect.自身速度减一:
                        user.AbilityLvDownChanged(StatType.Speed, 1, false);
                        break;
                    case MoveAdditionalEffect.自身双防减一:
                        user.AbilityLvDownChanged(StatType.Def, 1, false);
                        user.AbilityLvDownChanged(StatType.SpDef, 1, false);
                        break;
                    //case MoveAdditionalEffect.自身双防速度减一:
                    //    user.DefLVDown(1, true);
                    //    user.SDefLVDown(1, true);
                    //    user.SpeedLVDown(1, true, false);

                    //    break;
                    case MoveAdditionalEffect.次回合不能行动:
                        user.BattleState.nextTurnCantMove = true;
                        break;
                    case MoveAdditionalEffect.吸收一半伤害:
                        if (user.HealthPower != 0)
                        {
                            //食梦
                            if (target.GetSelTrait() == Trait.LiquidOoze && selMove.Data.Id != 138 && user.GetSelTrait() != Trait.MagicGuard)
                            {
                                log.AppendMsg("Hurt", user.CustomName);
                                //log.AppendText(target.GetNameString() + "的淤泥特性使" + user.CustomName + "受到了伤害！");
                                user.GetHurt(damage * 0.5);
                            }
                            else if (user.GetItem() == Item.BigRoot && selMove.Data.Id != 138)
                            {
                                user.HPRecover(damage / 3 * 2);
                                log.AppendMsg("HpRecover", user.CustomName);
                                //log.AppendText(user.CustomName + "吸收了对手的伤害！");
                            }
                            else
                            {
                                user.HPRecover(damage * 0.5);
                                log.AppendMsg("HpRecover", user.CustomName);
                                //log.AppendText(user.CustomName + "吸收了对手的伤害！");
                            }
                        }
                        break;
                }
                switch (selMove.Data.AddEffect2)
                {
                    case MoveAdditionalEffect.伤害反弹三分一:
                        if (user.GetSelTrait() != Trait.RockHead && user.GetSelTrait() != Trait.MagicGuard && damage != 0)
                        {
                            log.AppendMsg("ReHurt", user.CustomName);
                            //log.AppendText(user.CustomName + "受到了伤害反弹！"); 
                            user.GetHurt(damage / 3);
                        }
                        break;
                    case MoveAdditionalEffect.伤害反弹四分一:
                        if (user.GetSelTrait() != Trait.RockHead && user.GetSelTrait() != Trait.MagicGuard && damage != 0)
                        {
                            log.AppendMsg("ReHurt", user.CustomName);
                            //log.AppendText(user.CustomName + "受到了伤害反弹！");
                            user.GetHurt(damage * 0.25);
                        }
                        break;
                    case MoveAdditionalEffect.双刃头槌:
                        if (user.GetSelTrait() != Trait.RockHead && user.GetSelTrait() != Trait.MagicGuard && damage != 0)
                        {
                            log.AppendMsg("ReHurt", user.CustomName);
                            //log.AppendText(user.CustomName + "受到了伤害反弹！");
                            user.GetHurt(damage * 0.5);
                        }
                        break;
                    case MoveAdditionalEffect.绝境反击:
                        log.AppendMsg("ReHurt", user.CustomName);
                        //log.AppendText(user.CustomName + "受到了伤害反弹！");
                        user.HurtFraction(4);
                        break;
                    case MoveAdditionalEffect.蜻蜓回转:
                        if (user.CanChange() && user.HealthPower != 0)
                        {
                            user.BattleState.uTurn = true;
                            user.BattleState.pass = true;
                        }
                        break;
                    case MoveAdditionalEffect.滚动:
                        user.BattleState.Roll(selMove);
                        break;
                    case MoveAdditionalEffect.二至三回合攻击后混乱:
                        if (!user.BattleState.CrazyAttack)
                        {
                            user.BattleState.BeginCrazyAttack(_random, selMove);
                        }
                        break;
                    case MoveAdditionalEffect.吵闹:
                        if (!user.BattleState.CrazyAttack)
                        {
                            user.BattleState.BeginUproar(_random, selMove);
                        }
                        break;
                }
            }

            if (selMove.Data.AddEffect1 == MoveAdditionalEffect.高旋)
            {
                if (user.BattleState.constraint && user.BattleState.constraintTurn != -1)
                {
                    user.BattleState.constraint = false;
                    log.AppendMsg("DeDisable", user.CustomName);
                    //log.AddText(user.CustomName + "挣脱了束缚！");
                }
                user.BattleState.seed = false;
                user.BattleState.seedTarget = PokemonIndex.WrongInput;

                if (user.MyTeam.Group.spikesCounter > 0)
                {
                    user.MyTeam.Group.spikesCounter = 0;
                    log.AppendMsg("DeSpikes", user.MyTeam.PlayerName);
                }
                if (user.MyTeam.Group.poisonSpikesCounter > 0)
                {
                    user.MyTeam.Group.poisonSpikesCounter = 0;
                    log.AppendMsg("DeToxicSpikes", user.MyTeam.PlayerName);
                }
                if (user.MyTeam.Group.hideRockCounter > 0)
                {
                    user.MyTeam.Group.hideRockCounter = 0;
                    log.AppendMsg("DeStealthRock", user.MyTeam.PlayerName);
                }

                //log.AddText(user.MyTeam.PlayerName + "队伍周围的障碍被清除了！");
            }
            //if (user.BattleState.comboMoor)
            //{
            //    target.myTeam.MakeMoor();
            //}
            //else if (user.BattleState.comboRainbow)
            //{
            //    user.myTeam.MakeRainbow();
            //}
            //else if (user.BattleState.comboFlameSea)
            //{
            //    target.myTeam.MakeFlameSea();
            //}

            //switch (selMove.AddEff2)
            //{
            //    case MoveAdditionalEffect.溅射攻击:
            //        if (target.Friend != null)
            //        {
            //            target.Friend.GetHurt(target.Friend.MAXHP / 16);
            //            log.AddText(target.Friend.CustomName + "受到了溅射伤害！");
            //        }
            //        break;
            //    case MoveAdditionalEffect.交织火焰:
            //        if (target.HP != 0)
            //        {
            //            target.BattleState.crossFire = true;
            //        }
            //        break;
            //    case MoveAdditionalEffect.交织闪电:
            //        if (target.HP != 0)
            //        {
            //            target.BattleState.crossThunder = true;
            //        }
            //        break;
            //}
            EndAttack(user, selMove, target);
        }

        private void EndAttack(Pokemon user, Move selMove, Pokemon target)
        {
            if (target.HealthPower == 0)
            {
                if (target.BattleState.destinyBond)
                {
                    log.AppendMsg("DestinyBond", user.CustomName);
                    //log.AddText(user.CustomName + "与" + target.CustomName + "同行");
                    user.HurtFraction(1);
                }
                else if (target.BattleState.grudge)
                {
                    var move = user.GetSelMoves().First(m => m.Data.Id == selMove.Data.Id);
                    if (move != null)
                    {
                        move.PP = 0;
                        log.AppendMsg("Grudge", user.CustomName);
                        //log.AddText(target.CustomName + "的怨念使" + user.CustomName + "的" + selMove.Name + "技能PP变成了0");
                    }
                }
            }
        }
    }
}
